﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 天然气分页展示
    /// </summary>
    /// 创建时间：2022-12-7 10:25:27。作者：王浩力
    public partial class PageGasAnalysis : Page
    {
        public PageGasAnalysis()
        {
            InitializeComponent();
        }
        SourceDictBusiness sourceDictBusiness = new SourceDictBusiness();


        //删除，按钮。创建时间：2022-11-28 13:48:49
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;
            if (gas != null)
            {
                var result = MessageBox.Show("确认删除样品【" + gas.Sample_no + "】吗？", "删除确认", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    Gas_analysisBusiness bll = new Gas_analysisBusiness();
                    var resultDo = bll.Delete(gas.Id);
                    ShowAlertHelpter.ShowAlert(resultDo.Msg);
                    if (resultDo.Code == 200)
                    {
                        Page_Loaded(gas, null);
                    }
                }
            }
            // ShowAlertHelpter.ShowAlert("删除" + gas.Id,this);

        }

        //天然气录入。创建时间：2022-11-29 10:23:31。作者：王浩力
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            WindowAdd_Gasanalysis windowAdd_Gasanalysis = new WindowAdd_Gasanalysis();
            windowAdd_Gasanalysis.Closing += WindowAdd_Gasanalysis_Closing;
            windowAdd_Gasanalysis.ShowDialog();
        }

        //关闭时，刷新父列表数据。创建时间：2022-12-9 16:38:21。
        private void WindowAdd_Gasanalysis_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //ShowAlertHelpter.ShowAlert("刷新父页面");
            DataGridLoadData(pager.PageIndex);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //状态查询条件         
            cmb_Step.ItemsSource = CommonConfig.StepList;
            cmb_Step.DisplayMemberPath = "Value";
            cmb_Step.SelectedValuePath = "Key";

            DataGridLoadData(1);
        }

        private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            //ShowAlertHelpter.ShowAlert(e.);
        }

        //查询；分页。创建时间：2022-12-7 18:08:36。作者：王浩力
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            DataGridLoadData(1);
        }

        //点击页码
        private void PageBar_PageIndexChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
        {
            //ShowAlertHelpter.ShowAlert("页码：" + e.NewValue, this);
            DataGridLoadData(e.NewValue);
        }

        /// <summary>
        /// 加载表格数据
        /// </summary>
        /// <param name="pageIndex">查询的页码</param>
        /// 创建时间：2022-12-8 17:25:45。作者：王浩力
        private void DataGridLoadData(int pageIndex)
        {
            Gas_analysis gas_Analysis = new Gas_analysis();
            gas_Analysis.Sample_no = txtSampleNo.Text.Trim();
            gas_Analysis.E1 = 0;//不返回删除记录
            gas_Analysis.E3 = date1.Text;//开始日期
            gas_Analysis.E4 = date2.Text;//结束日期

            //步骤值，  //创建时间：2023-1-11 12:48:51
            if (cmb_Step.SelectedValue != null && byte.TryParse(cmb_Step.SelectedValue.ToString(), out byte step))
            {
                gas_Analysis.O_step = step;
            }

            Task.Run(() =>
           {
               Gas_analysisBusiness bll = new Gas_analysisBusiness();
               var page = bll.Page(gas_Analysis, pageIndex, CommonConfig.PageSize);

               this.Dispatcher.InvokeAsync(() =>
               {
                   this.dataGrid.ItemsSource = page.List;
                   this.pager.Total = page.Total;
                   this.pager.PageSize = page.PageSize;
                   this.pager.PageIndex = pageIndex;
               });
           });

        }

        //校对
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;

            WindowProofreadGasanalysis windowProofread = new WindowProofreadGasanalysis();
            windowProofread.gas_Analysis = gas;
            windowProofread.Closed += WindowProofread_Closed;
            windowProofread.ShowDialog();
        }

        private void WindowProofread_Closed(object? sender, EventArgs e)
        {
            //ShowAlertHelpter.ShowAlert("关闭子窗口，刷新父页面列表",this);
            DataGridLoadData(pager.PageIndex);
        }

        //审核
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;

            WindowGasCheck windowGasCheck = new WindowGasCheck();
            windowGasCheck.gas_Analysis = gas;

            //审核关闭窗口后，刷新父窗口数据  //创建时间：2022-12-9 18:21:06
            windowGasCheck.Closing += WindowProofread_Closed;

            windowGasCheck.ShowDialog();
        }

        //编辑    //创建时间：2022-12-11 21:25:06。作者：王浩力
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;

            WindowGasEdit windowGasEdit = new WindowGasEdit();
            windowGasEdit.gas_Analysis = gas;

            windowGasEdit.ShowDialog();
        }

        //日志
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;

            WindowGasStepLog log = new WindowGasStepLog();
            log.gasKey = gas.Id;
            log.ShowDialog();
        }

        //打印预览。//创建时间：2022-12-12 16:00:25。作者：王浩力
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;

            //查询完整数据
            Gas_analysisBusiness busi = new Gas_analysisBusiness();
            var model = busi.GetInfoStr(gas.Id);

            //天然气，备注 ，标准。创建时间：2023-1-11 14:42:34
            var biaozhunList = sourceDictBusiness.GetListByGroup("gas_bz_remark_list", 1);
            biaozhunList = biaozhunList.OrderBy(g => g.Createtime).ToList();
            model.source_Dicts = biaozhunList;

            try
            {
                WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("DocumentGasPrint.xaml", model, new PrintGas_analysisDocument());

                //注释时间：2023-1-8 14:49:42。修改人：王浩力
                //显示签名控制
                //ppv.SignNameShow += Ppv_SignNameShow; 

                ppv.ShowDialog();
            }
            catch (Exception ex)
            {
                var ex1 = ex.InnerException ?? ex;
                LogHelpter.AddLog("出错:" + ex1.Message, null, "PrintViewError");
                //ShowAlertHelpter.ShowAlert("出错:" + ex1.Message);
            }
        }

        //注释时间：2023-1-8 14:49:30。作者：王浩力
        ////显示签名 控制
        //private void Ppv_SignNameShow(object? sender, EventArgs e)
        //{
        //    FlowDocument doc = sender as FlowDocument;
        //    if (doc == null) return;

        //    //Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;

        //    //Gas_analysisBusiness busi = new Gas_analysisBusiness();
        //    //var model = busi.GetInfoStr(gas.Id);

        //    //if (doc.Tag != null && doc.Tag.ToString() == "1") //显示签名
        //    //{
        //    //    //不显示，分析人姓名
        //    //    model.Analysis_user = null;               
        //    //}
        //    //else// 不显示签名
        //    //{
        //    //    model.E6 = null;//分析人，电子签名
        //    //    model.E7 = null;//校对人，电子签名
        //    //    model.E8 = null;//审核人，电子签名
        //    //}
        //    //doc.Tag = "INS-切换了";
        //     //doc.DataContext = model;
        //}

        //导出
        private void Export_Click(object sender, RoutedEventArgs e)
        {
            btnExport.IsEnabled = false;
            btnExport.Content = "处理中...";

            Gas_analysis gas_Analysis = new Gas_analysis();
            gas_Analysis.Sample_no = txtSampleNo.Text.Trim();
            gas_Analysis.E1 = 0;//不返回删除记录
            gas_Analysis.E3 = date1.Text;//开始日期
            gas_Analysis.E4 = date2.Text;//结束日期

            Task.Run(() =>
            {
                //System.Threading.Thread.Sleep(1000*20);

                Gas_analysisBusiness busi = new Gas_analysisBusiness();

                //生成excel文件
                var res = busi.ExportNew(gas_Analysis);
                ShowAlertHelpter.ShowAlert(res.Msg);
                this.Dispatcher.Invoke(() =>
                {
                    btnExport.Content = "导出";
                    btnExport.IsEnabled = true;
                });
                if (res.CodeEnum == ResultCode.Success)
                {
                    if (res.Data != null)
                    {
                        System.Diagnostics.Process.Start("explorer", res.Data.ToString());
                    }
                }
            });
        }

        //导出成A2这个，只需要类别是开发或者勘探的
        //创建时间：2023-2-21 17:39:21。作者：王浩力
        private void ExportA2_Click(object sender, RoutedEventArgs e)
        {
            btnExportA2.IsEnabled = false;
            btnExportA2.Content = "处理中...";

            Gas_analysis gas_Analysis = new Gas_analysis();
            gas_Analysis.Sample_no = txtSampleNo.Text.Trim();
            gas_Analysis.E1 = 0;//不返回删除记录
            gas_Analysis.E3 = date1.Text;//开始日期
            gas_Analysis.E4 = date2.Text;//结束日期

            //只导出开发，勘探的  //添加时间：2023-3-2 09:44
             gas_Analysis.E8 = "1,3";

            Task.Run(() =>
            {
                //System.Threading.Thread.Sleep(1000*20);

                Gas_analysisBusiness busi = new Gas_analysisBusiness();

                //生成excel文件
                var res = busi.ExportExcelToA2(gas_Analysis);
                ShowAlertHelpter.ShowAlert(res.Msg);
                this.Dispatcher.Invoke(() =>
                {
                    btnExportA2.Content = "导出A2";
                    btnExportA2.IsEnabled = true;
                });
                if (res.CodeEnum == ResultCode.Success)
                {
                    if (res.Data != null)
                    {
                        System.Diagnostics.Process.Start("explorer", res.Data.ToString());
                    }
                }
            });
        }




        ///// <summary>
        ///// 表格中，按了ctrl+c表示复制 编号
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void dataGrid_KeyDown(object sender, KeyEventArgs e)
        //{

        //    //if (e.Key == Key.LeftCtrl && e.Key == Key.C)

        //    //同时按下了Ctrl+C
        //    //if (Keyboard.IsKeyDown(Key.C) && Keyboard.IsKeyDown(Key.LeftCtrl))
        //    //if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.C))
        //    if (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.C)
        //    {
        //        Gas_analysis? gas = this.dataGrid.SelectedItem as Gas_analysis;
        //        if (gas == null)
        //        {
        //            e.Handled = true;
        //            return;
        //        }
        //        e.Handled = true;
        //        ShowAlertHelpter.ShowAlert(gas.Sample_no);
        //    }
        //}


    }
}
